package com.macro.mall.tiny.modules.foster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.mapper.PetMapper;
import com.macro.mall.tiny.modules.foster.mapper.PetStatusMapper;
import com.macro.mall.tiny.modules.foster.mapper.PetStatusPictureMapper;
import com.macro.mall.tiny.modules.foster.model.Pet;
import com.macro.mall.tiny.modules.foster.model.PetStatus;
import com.macro.mall.tiny.modules.foster.model.PetStatusPicture;
import com.macro.mall.tiny.modules.foster.model.domain.AddressAndTime;
import com.macro.mall.tiny.modules.foster.model.domain.Pictures;
import com.macro.mall.tiny.modules.foster.model.dto.PetStatusDTO;
import com.macro.mall.tiny.modules.foster.model.vo.PetStatusVO;
import com.macro.mall.tiny.modules.foster.service.PetStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhuyi
 * @since 2024-03-30
 */
@Service
public class PetStatusServiceImpl extends ServiceImpl<PetStatusMapper, PetStatus> implements PetStatusService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PetStatusServiceImpl.class);

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetStatusMapper petStatusMapper;
    @Autowired
    private PetStatusPictureMapper petStatusPictureMapper;

    /**
     * 添加宠物状态和图片信息
     *
     * @param petStatusDTO
     * @return
     */
    @Override
    public CommonResult savePetStatus(PetStatusDTO petStatusDTO) {
        //删除这个宠物之前的状态
        Long petId = petStatusDTO.getPetStatus().getPetId();
        QueryWrapper<PetStatus> wrapper = new QueryWrapper<>();
        wrapper
                .eq("pet_id", petId);
        petStatusMapper.delete(wrapper);
        //删除这个宠物之前的宠物状态图片
        QueryWrapper<PetStatusPicture> pictureWrapper = new QueryWrapper<>();
        pictureWrapper
                .eq(true,"pet_id", petId);
        petStatusPictureMapper.delete(pictureWrapper);

        //插入宠物状态
        PetStatus petStatus = petStatusDTO.getPetStatus();
        petStatus.setId(getId());
        petStatusMapper.insert(petStatus);

        //插入宠物状态图片
        petStatusDTO.getPetStatusPictures().stream().forEach(petStatusPicture -> {
            petStatusPicture.setId(getId());
            setCurrentTime(petStatusPicture);
            petStatusPictureMapper.insert(petStatusPicture);
        });

        LOGGER.info("添加宠物状态和图片信息成功");
        return CommonResult.success("添加宠物状态和图片信息成功");
    }

    /**
     * 获取指定宠物id的宠物状态
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult selectByPetId(Long id) {
        Pet pet = petMapper.selectById(id);
        QueryWrapper<PetStatus> wrapper = new QueryWrapper<>();
        wrapper
                .eq("pet_id", pet.getId());
        PetStatus petStatus = petStatusMapper.selectOne(wrapper);
        //获取宠物状态图片列表
        QueryWrapper<PetStatusPicture> pictureWrapper = new QueryWrapper<>();
        pictureWrapper
                .eq("pet_id", pet.getId());
        List<PetStatusPicture> petStatusPictures = petStatusPictureMapper.selectList(pictureWrapper);
        //转换为宠物状态VO
        PetStatusVO petStatusVO = new PetStatusVO();
        petStatusVO.setIsFeed(petStatus.getIsFeed());
        petStatusVO.setIsInject(petStatus.getIsInject());
        petStatusVO.setIsShower(petStatus.getIsShower());
        //创建图片列表对象
        Pictures pictures = new Pictures();
        ArrayList<AddressAndTime> feed = new ArrayList<>();
        ArrayList<AddressAndTime> walkDog = new ArrayList<>();
        ArrayList<AddressAndTime> walkCat = new ArrayList<>();
        ArrayList<AddressAndTime> inject = new ArrayList<>();
        ArrayList<AddressAndTime> shower = new ArrayList<>();
        //遍历宠物状态图片列表
        petStatusPictures.stream().forEach(petStatusPicture -> {
            Integer type = petStatusPicture.getType();
            switch (type) {
                case 0:
                    insertAddressAndTime(petStatusPicture,feed);
                    break;
                case 1:
                    insertAddressAndTime(petStatusPicture,walkDog);
                    break;
                case 2:
                    insertAddressAndTime(petStatusPicture,walkCat);
                    break;
                case 3:
                    insertAddressAndTime(petStatusPicture,inject);
                    break;
                case 4:
                    insertAddressAndTime(petStatusPicture,shower);
                    break;
                default:
                    CommonResult.failed("未知类型");
            }
            petStatusVO.setPictures(pictures);
        });

        pictures.setFeed(feed);
        pictures.setWalkDog(walkDog);
        pictures.setWalkCat(walkCat);
        pictures.setInject(inject);
        pictures.setShower(shower);
        petStatusVO.setPictures(pictures);
        petStatusVO.setStatusId(petStatus.getId());
        LOGGER.info("获取指定宠物id的宠物状态成功!");
        return CommonResult.success(petStatusVO);
    }

    /**
     * 修改宠物状态和图片
     *
     * @param petStatusDTO
     * @return
     */
    @Override
    public CommonResult updateStatus(PetStatusDTO petStatusDTO) {
        PetStatus petStatus = petStatusDTO.getPetStatus();
        petStatusMapper.updateById(petStatus);
        QueryWrapper<PetStatusPicture> wrapper = new QueryWrapper<>();
        wrapper
                .eq("pet_id", petStatus.getPetId());
        petStatusPictureMapper.delete(wrapper);
        //插入宠物状态图片
        petStatusDTO.getPetStatusPictures().stream().forEach(petStatusPicture -> {
            petStatusPicture.setId(getId());
            setCurrentTime(petStatusPicture);
            petStatusPicture.setPetId(petStatus.getPetId());
            petStatusPictureMapper.insert(petStatusPicture);
        });
        LOGGER.info("宠物状态修改成功");
        return CommonResult.success("宠物状态修改成功");
    }

    /**
     * 在图片表中插入现在的时间
     *
     * @param petStatusPicture
     */
    private static void setCurrentTime(PetStatusPicture petStatusPicture) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        petStatusPicture.setTime(formatter.format(calendar.getTime()));
    }

    /**
     * 在列表中插入图片地址和时间
     *
     * @param petStatusPicture
     * @param prop
     */
    private static void insertAddressAndTime(PetStatusPicture petStatusPicture, ArrayList<AddressAndTime> prop) {
        AddressAndTime addressAndTime = new AddressAndTime();
        addressAndTime.setUrl(petStatusPicture.getUrl());
        addressAndTime.setTime(petStatusPicture.getTime());
        prop.add(addressAndTime);
    }
}
